<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div lay-filter="listSearch" class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">

                {neq name="role_name" value="channel"}
                <div class="layui-inline">
                    <select name="channel_id" lay-search>
                        <option value="0">请选择投放号</option>
                        {volist name="channels" id="v"}
                        <option value="{$v.id}">{$v.account}</option>
                        {/volist}
                    </select>
                </div>
                {/neq}

                <div class="layui-inline">
                    <select name="type">
                        <option value="0">请选择成本类型</option>
                        <option value="1">投放号</option>
                        <option value="2">投放媒体</option>
                        <option value="3">推广链接</option>
                    </select>
                </div>

                <div class="layui-inline">
                    <input type="text" id="dayStart" name="cost_date" placeholder="请选择消耗日期" autocomplete="off" class="layui-input">
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-list-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="LAY-reset">重置</button>
                </div>

            </div>
        </div>

        <div class="layui-card-body">

            <div class="layui-inline" style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>

            <table id="LAY-list-manage" lay-filter="LAY-list-manage"></table>


            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.use('laydate', function () {
        var $ = layui.$;
        var laydate = layui.laydate;

        var nowTime = new Date().valueOf();
        var start = laydate.render({
            elem: '#dayStart',
            type: 'date',
            max: nowTime,
            done: function (value, date) {
    
            }
        });
    });

    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','admin'], function(){
        var $ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,table = layui.table;

        //监听搜索
        form.on('submit(LAY-list-front-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-list-manage', {
                where: field
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        form.on('submit(LAY-reset)', function(data){
            form.val("listSearch", {
                "channel_id": "0",
                "cost_date": "",
                "type": "0"
            });
            form.render();
            table.reload('LAY-list-manage', {
                where: {"channel_id": "0", "cost_date": "", "type": "0"}
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        table.render({
            elem: '#LAY-list-manage'
            , url: "{:url('Cost/list_data')}" //模拟接口
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            , page: true //开启分页
            , limit: 20
            , cols: [[
                {field: 'cost_date', minWidth: 120, title: '消耗日期', align: 'center'}
                , {field: 'type_name', title: '成本类型', align: 'center'}
                , {field: 'money', title: '消耗金额', align: 'center'}
                , {field: 'channel_name', title: '投放号', align: 'center'}
                , {field: 'media_name', title: '投放媒体', align: 'center'}
                , {field: 'link_name', title: '推广链接', align: 'center'}
                , {title: '操作', width: 170, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
        });

        //监听工具条
        table.on('tool(LAY-list-manage)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改广告成本'
                    ,offset: '15px'
                    ,content: "{:url('Cost/preview')}?id="+obj.data.id
                    ,area: ['580px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-data-edit-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        layer.msg('请稍候，正在执行相关报表数据更新');

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            $.ajax({
                                url:"{:url('Cost/edit')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg(rs['msg'],function () {
                                            layer.close(index); //关闭弹层
                                            table.reload('LAY-list-manage');
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });

                        });

                        submit.trigger('click');
                    }
                })
            }else if (obj.event === 'del'){
                layer.confirm('确定删除吗？', function (index) {

                    $.ajax({
                        url: "{:url('Cost/delete')}?id=" + obj.data.id,
                        type: "get",
                        dataType: "json",
                        data: {},
                        async: true,
                        success: function (rs) {
                            if (rs['code'] == 100) {

                                layer.msg(rs['msg'], function () {
                                    obj.del();
                                    layer.close(index);
                                });

                            } else {
                                layer.msg(rs['msg']);
                            }
                            return false;
                        }
                    });

                });

            }
        });


        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加广告成本'
                    ,offset: '15px'
                    ,content: "{:url('Cost/add')}"
                    ,area: ['580px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-data-add-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        layer.msg('请稍候，正在执行相关报表数据更新');

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('Cost/add')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg(rs['msg'],function () {
                                            layer.close(index); //关闭弹层
                                            table.reload('LAY-list-manage');
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>